<?php
/**
 * Created by PhpStorm.
 * User: 87393
 * Date: 2019-03-26
 * Time: 23:15
 */

namespace App\Http\Repositories\Services\Impls;


use App\Exceptions\ServiceException;
use App\Http\Constants\CommonExceptionConstants;
use App\Http\Repositories\Daos\IRoleDao;
use App\Http\Repositories\Daos\ISystemDao;
use App\Http\Repositories\Eloquent\System;
use App\Http\Repositories\Services\ISystem;
use App\Http\Requests\Admin\LoginDTO;
use App\Http\Requests\Admin\SystemInputDTO;
use App\Util\Helper;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;

class SystemService implements ISystem
{

    protected $systemDao;
    protected $roleDao;

    public function __construct(ISystemDao $systemDao,IRoleDao $roleDao)
    {
        $this->systemDao = $systemDao;
        $this->roleDao = $roleDao;
    }

    public function login(LoginDTO $loginDTO)
    {
        $data = $this->systemDao->byName($loginDTO->post('name'));
        if (!$data){
            throw new ServiceException(CommonExceptionConstants::getKey('no_find_data'));
        }
        if ($data->password != md5(md5($loginDTO->get('password')).$data->salt)) {
            throw new ServiceException(CommonExceptionConstants::getKey('password_error'));
        }
        if(!$data->state){
            throw new ServiceException(CommonExceptionConstants::getKey('ban_login'));
        }
        $data = Helper::arrayField($data->toArray(), [], ['password','salt','created_at','updated_at']);
        $loginDTO->session()->put('admin_user',$data);
        $loginDTO->session()->save();
    }

    public function store(SystemInputDTO $inputDTO)
    {

        if (empty($inputDTO->post('password'))) {
            throw new ServiceException(CommonExceptionConstants::getKey('password_required'));
        }

        $data = $this->systemDao->byName($inputDTO->post('name'));
        if ($data){
            throw new ServiceException(CommonExceptionConstants::getKey('user_name_was_used'));
        }

        $dao = new System();
        $dao->role_id = $inputDTO->post('role_id');
        $dao->name = $inputDTO->post('name');
        $dao->real_name = $inputDTO->post('real_name');
        $dao->password = $inputDTO->post('password');
        $dao->pen_name = '';
        $dao->media = '';
        $dao->salt = Helper::random(4);
        $dao->password = md5(md5($dao->password).$dao->salt);

        $dao->save();
    }

    public function update(SystemInputDTO $inputDTO, $id)
    {

        $data = $this->systemDao->byName($inputDTO->post('name'), $id);
        if ($data){
            throw new ServiceException(CommonExceptionConstants::getKey('user_name_was_used'));
        }

        $dao = $this->systemDao->find($id);
        if (!$dao) {
            throw new ServiceException(CommonExceptionConstants::getKey('no_find_data'));
        }

        $dao->role_id = $inputDTO->post('role_id');
        $dao->real_name = $inputDTO->post('real_name');
        $dao->name = $inputDTO->post('name');

        if ($inputDTO->post('password')) {
            $dao->password = md5(md5($inputDTO->post('password')).$dao->salt);
        }

        $dao->save();

    }

    public function byId($id)
    {
        $data = $this->systemDao->find($id);
        if (!$data) {
            throw new ServiceException(CommonExceptionConstants::getKey('no_find_data'));
        }
        return $data;
    }

    public function lists($search, $page, $size = 10)
    {

        $data = $this->systemDao->lists($search, $page, $size);

        $roleDao = $this->roleDao->all();
        $roleDao  = Helper::arrayMapByKey($roleDao,'id','name');
        if ($data['total']) {
            foreach ($data['results'] as $key => $val){
                $data['results'][$key] = $val;
                if ($roleDao) {
                    $data['results'][$key]['role_name'] = $roleDao[$val['role_id']];
                } else {
                    $data['results'][$key]['role_name'] = '';
                }
                //$data['results'][$key]['state_name'] = Config::get('constant.state.'.$val['state']);
            }
        }

        return $data;

    }

    public function state($id, $state)
    {
        $where = ['id'=>['symbol'=>'in','val'=>explode(',',$id)]];
        $this->systemDao->update($where, ['state'=>$state]);
    }

}